<template>
  <div class="register-container">
    <!-- 注册卡片 -->
    <div class="register-card-container">
      <!-- 卡片装饰 -->
      <div class="card-shape top-shape"></div>
      <div class="card-shape bottom-shape"></div>
      
      <div class="register-card">
        <div class="logo-section">
          <div class="logo-container">
            <img src="../assets/images/index-logo.png" alt="Logo" class="logo-image">
            <div class="logo-glow"></div>
          </div>
          <h2 class="system-name">DIMS-HSRC</h2>
          <div class="title-divider">
            <span class="divider-dot"></span>
            <span class="divider-line"></span>
            <span class="divider-dot"></span>
          </div>
          <h3 class="system-title">需求驱动型高铁智能合同生成系统</h3>
        </div>

        <el-form 
          ref="registerForm" 
          :model="registerForm" 
          :rules="registerRules" 
          class="register-form"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username" 
              type="text" 
              auto-complete="off" 
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password" 
              type="password" 
              auto-complete="off" 
              placeholder="请输入密码" 
              prefix-icon="el-icon-lock"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword" 
              type="password" 
              auto-complete="off" 
              placeholder="请确认密码" 
              prefix-icon="el-icon-lock"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item prop="code" v-if="captchaEnabled">
            <div class="captcha-container">
              <el-input 
                v-model="registerForm.code" 
                auto-complete="off" 
                placeholder="请输入验证码" 
                prefix-icon="el-icon-key"
                class="custom-input captcha-input"
              />
              <div class="captcha-img-container" @click="getCode">
                <img :src="codeUrl" alt="验证码" class="captcha-img"/>
                <div class="captcha-refresh-hint">点击刷新</div>
              </div>
            </div>
          </el-form-item>
          
          <div class="form-options">
            <div></div>
            <router-link class="login-link" to="/login">使用已有账户登录</router-link>
          </div>
          
          <div class="btn-container">
            <el-button 
              :loading="loading" 
              type="primary" 
              class="register-button" 
              @click.native.prevent="handleRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注册中...</span>
            </el-button>
          </div>
        </el-form>
      </div>
    </div>

    <!-- 底部版权信息 -->
    <div class="footer">
      <div class="footer-content">
        <span class="copyright">© 2025 西南交通大学 版权所有</span>
        <div class="footer-decoration"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
/* 整体容器 */
.register-container {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  overflow: hidden;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
}

/* ====== 注册卡片 ====== */
.register-card-container {
  position: relative;
  z-index: 10;
  width: 420px;
  max-width: 90%;
}

.register-card {
  position: relative;
  padding: 30px 35px;
  border-radius: 15px;
  background-color: rgba(220, 220, 220, 0.75);
  backdrop-filter: blur(15px);
  box-shadow: 
    0 15px 35px rgba(0, 0, 0, 0.25),
    0 5px 15px rgba(0, 0, 0, 0.1),
    inset 0 0 0 1px rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(200, 200, 200, 0.3);
  overflow: hidden;
}

.card-shape {
  position: absolute;
  background-color: rgba(180, 180, 180, 0.5);
  border-radius: 50%;
  z-index: -1;
}

.top-shape {
  width: 150px;
  height: 150px;
  top: -75px;
  right: -75px;
}

.bottom-shape {
  width: 200px;
  height: 200px;
  bottom: -100px;
  left: -100px;
}

/* ====== Logo Section ====== */
.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 25px;
}

.logo-container {
  position: relative;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-image {
  width: 80px;
  height: 80px;
  object-fit: contain;
  position: relative;
  z-index: 2;
}

.logo-glow {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 70%);
  filter: blur(5px);
  opacity: 0.7;
  z-index: 1;
}

.system-name {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 10px;
  color: #333;
  letter-spacing: 1px;
}

.title-divider {
  display: flex;
  align-items: center;
  width: 80px;
  margin: 0 0 10px;
}

.divider-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: #4b77e0;
}

.divider-line {
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, #4b77e0, #1a3a8f);
  margin: 0 5px;
}

.system-title {
  font-size: 16px;
  font-weight: 400;
  color: #555;
  margin: 0;
  letter-spacing: 0.5px;
}

/* ====== Form Styles ====== */
.register-form {
  width: 100%;
}

.custom-input {
  .el-input__inner {
    height: 40px;
    line-height: 40px;
    border-radius: 8px;
    border: 1px solid #dcdfe6;
    transition: all 0.3s;
    
    &:hover {
      border-color: #c0c4cc;
    }
    
    &:focus {
      border-color: #4b77e0;
      box-shadow: 0 0 0 2px rgba(75, 119, 224, 0.2);
    }
  }
  
  .el-input__prefix {
    left: 10px;
    color: #909399;
  }
  
  .el-input__inner {
    padding-left: 35px;
  }
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-input {
  flex: 1;
}

.captcha-img-container {
  position: relative;
  width: 130px;
  height: 40px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 1px solid #dcdfe6;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  
  &:hover .captcha-refresh-hint {
    opacity: 1;
  }
}

.captcha-img {
  height: 100%;
  width: 100%;
  object-fit: cover;
  display: block;
}

.captcha-refresh-hint {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 4px 0;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 12px;
  text-align: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 15px 0 25px;
}

.login-link {
  color: #4b77e0;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.3s ease;
  
  &:hover {
    color: #1a3a8f;
    text-decoration: underline;
  }
}

.btn-container {
  display: flex;
  justify-content: center;
}

.register-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  letter-spacing: 1px;
  border-radius: 8px;
  background: linear-gradient(135deg, #4b77e0, #1a3a8f);
  border: none;
  box-shadow: 0 4px 12px rgba(75, 119, 224, 0.4);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(75, 119, 224, 0.5);
    background: linear-gradient(135deg, #5d89f2, #204ab6);
  }
  
  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(75, 119, 224, 0.3);
  }
}

/* ====== Footer ====== */
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 15px 0;
  z-index: 10;
}

.footer-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.copyright {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  letter-spacing: 1px;
}

.footer-decoration {
  width: 150px;
  height: 2px;
  background: linear-gradient(90deg, 
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.5) 50%,
    rgba(255, 255, 255, 0) 100%
  );
}

/* ====== Responsive Adjustments ====== */
@media (max-width: 768px) {
  .register-card {
    padding: 25px 20px;
  }
  
  .system-name {
    font-size: 24px;
  }
  
  .system-title {
    font-size: 14px;
  }
  
  .logo-container {
    width: 70px;
    height: 70px;
  }
  
  .logo-image {
    width: 70px;
    height: 70px;
  }
}
</style>
